package com.controller;

import com.api.CommonResult;
import com.entity.BType;
import com.entity.BUser;
import com.mybatisflex.core.paginate.Page;
import com.mybatisflex.core.query.QueryWrapper;
import com.service.impl.BUserServiceImpl;
import jakarta.servlet.http.HttpSession;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;

@Controller
@RequestMapping("/admin")
public class AdminController {

    @Autowired
    private BUserServiceImpl bUserService;

    @GetMapping("/login")
    public String toAdminLoginPage() {
        return "back/login";
    }

    @PostMapping("/toLogin")
    public String toAdminLogin(@ModelAttribute BUser bUser, HttpSession session) {
        BUser res = bUserService.selectBUser(bUser);
        if (res == null) {
            return toAdminLoginPage();
        } else {
            session.setAttribute("admin", res); //设置登录信息
            return "back/index";
        }
    }

    @GetMapping("/welcome")
    public String toWelcome() {
        return "back/welcome";
    }

    @GetMapping("/list")
    public String userList(ModelMap model,
                           @RequestParam(value = "p", defaultValue = "1") int p,
                           @RequestParam(value = "pageSize", defaultValue = "10") int pageSize) {
        //按照用户名查询
        QueryWrapper queryWrapper = QueryWrapper.create();
        Page<BUser> page = new Page<>();
        page.setPageSize(pageSize);
        page.setPageNumber(p);
        Page<BUser> pageInfo = bUserService.page(page, queryWrapper);
        model.addAttribute("page", pageInfo);
        model.addAttribute("total", pageInfo.getTotalRow());
        return "back/admin-list";
    }

    @GetMapping("/addView")
    public String toAddViewPage() {
        return "back/admin-add";
    }

    @PostMapping("/save")
    @ResponseBody
    public CommonResult save(@ModelAttribute BUser bUser) {
        System.out.println(bUser);
        try {
            Boolean r = bUserService.save(bUser);
            return CommonResult.success(r);
        } catch (Exception e) {
            e.printStackTrace();
            String msg = bUser.getUsername() + "已存在";
            return CommonResult.failed(msg);
        }
    }

    @GetMapping("/updateView")
    public String updateBTypeView(int id, ModelMap model) {
        BUser bUser = bUserService.getById(id);
        System.out.println(bUser);
        model.addAttribute("user", bUser);
        return "back/admin-edit";
    }

    @PostMapping("/update")
    @ResponseBody
    public CommonResult update(@ModelAttribute BUser bUser) {
        System.out.println(bUser);
        try {
            Boolean r = bUserService.updateById(bUser);
            return CommonResult.success(r);
        } catch (Exception e) {
            e.printStackTrace();
            String msg = bUser.getUsername() + "已存在";
            return CommonResult.failed(msg);
        }
    }

    @PostMapping("/del")
    @ResponseBody
    public CommonResult del(int id) {
        try {
            Boolean r = bUserService.removeById(id);
            return CommonResult.success(r);
        } catch (Exception e) {
            e.printStackTrace();
            return CommonResult.failed(e.getMessage());
        }
    }

}
